package org.devbg.academy.corejava.shop.ui.models;

import java.util.Collections;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.devbg.academy.corejava.shop.db.daos.OrderItemDAO;
import org.devbg.academy.corejava.shop.db.entities.Order;
import org.devbg.academy.corejava.shop.db.entities.OrderItem;


public class OrderItemTableModel extends AbstractTableModel{

	private static final long serialVersionUID = 1L;
	
	private OrderItemDAO dao = new OrderItemDAO();
	private List<OrderItem> items = Collections.<OrderItem> emptyList();

	public void changeOrder(Order order) {
		this.items = dao.findByOrder(order);
		fireTableDataChanged();
	}

	private static final String[] COLUMN_NAMES = new String[] { "ID", "Продукт", "Количество" };
	
	public String getColumnName(int column) {
		return COLUMN_NAMES[column];
	}
	
	public OrderItemTableModel() {
		
	}

	@Override
	public int getColumnCount() {
		return COLUMN_NAMES.length;
	}

	@Override
	public int getRowCount() {
		return items.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return getValue(items.get(rowIndex), columnIndex);
	}

	private Object getValue(OrderItem item, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return item.getId();
		case 1:
			return item.getProduct().getName();
		case 2:
			return item.getQuantity();
		default:
			return null;
		}
	}
}


